/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.helper;

import java.util.ArrayList;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.preference.CommonPreferenceConstants;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Factory for a {@link org.eclipse.uml2.uml.Comment}.
 * 
 * @author Patrick Tessier
 */
public class NodeCommentHelper extends NodeHelper {

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#createGraphElement(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.GraphElement, org.eclipse.draw2d.geometry.Point, org.eclipse.draw2d.geometry.Dimension)
	 */
	/**
	 * 
	 * 
	 * @param umlChild 
	 * @param size 
	 * @param location 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public GraphNode createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size){
		Debug.debug(this,"Call NodeCommentFactory.createGraphNode()", channel); //$NON-NLS-1$
		
		// set color for default created element
		GraphNode graphNode=super.createGraphElement(umlChild, null, location, size);
		
		//the ge parent is connected in last because an event may be sent at this moment and trigger a refresh.
		parent.getContained().add(graphNode);
		return graphNode;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#createGraphNode(org.eclipse.uml2.uml.NamedElement, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
	/**
	 * 
	 * 
	 * @param umlElement 
	 * @param diagram 
	 * 
	 * @return 
	 */
	@Override
	public ArrayList createGraphNode(Element umlElement, Diagram diagram) {
		Debug.debug(this,"Call NodeCommentFactory.createGraphNode()", channel); //$NON-NLS-1$
		return new ArrayList();
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.factories.NodeFactory#canExecuteCreateCommand(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement parent){
		if(parent instanceof GraphEdge) {
			return false;
		}
		return true;
	}

    ///////////////////////////////////////////////////////////////////////////
    // UML Management
    ///////////////////////////////////////////////////////////////////////////
	
    /* (non-Javadoc)
     * @see com.cea.papyrus.core.common.factories.NodeFactory#createElement(java.lang.String, org.eclipse.uml2.uml.Element)
     */
    /**
     * 
     * 
     * @param umlParent 
     * @param body 
     * 
     * @return 
     */
    @Override
    public org.eclipse.uml2.uml.Element createUMLElement(String body, org.eclipse.uml2.uml.Element umlParent) {
	Debug.debug(this,"Call ModelResourceManager.Comment()", channel); //$NON-NLS-1$
	//create PrimitiveType
	Comment aComment = umlParent.createOwnedComment();
	aComment.setBody(body);
	(umlParent).getOwnedComments().add(aComment);
	return aComment;
    }

    /* (non-Javadoc)
     * @see com.cea.papyrus.core.common.factories.NodeFactory#canExecuteCreateElement(org.eclipse.uml2.uml.Element)
     */
    /**
     * 
     * 
     * @param umlParent 
     * 
     * @return 
     */
    @Override
    public boolean canCreateUMLElement(Element umlParent) {
    	// can be created everywhere
    	return true;
    }

    /* (non-Javadoc)
     * @see com.cea.papyrus.core.common.factories.NodeFactory#addElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
     */
    /**
     * 
     * 
     * @param element 
     * @param umlParent 
     * 
     * @return 
     */
    @Override
    public boolean addUMLElement(Element element, Element umlParent) {
	return umlParent.getOwnedComments().add((Comment)element);
    }

    /* (non-Javadoc)
     * @see com.cea.papyrus.core.common.factories.NodeFactory#removeElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
     */
    /**
     * 
     * 
     * @param element 
     * @param umlParent 
     * 
     * @return 
     */
    @Override
    public boolean removeUMLElement(Element element, Element umlParent) {
	return umlParent.getOwnedComments().remove(element);
    }

    /* (non-Javadoc)
     * @see com.cea.papyrus.core.common.factories.NodeFactory#getBaseString()
     */
    /**
     * 
     * 
     * @return 
     */
    @Override
    public String getBaseString() {
	return "Comment"; //$NON-NLS-1$
    }

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.helper.NodeHelper#setGraphElementPreferences(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	/**
	 * 
	 * 
	 * @param graphElement 
	 */
	@Override
	public void setGraphElementPreferences(GraphElement graphElement) {
		// Get the preference store
		IPreferenceStore store = PapyrusPlugin.getDefault().getPreferenceStore();
		
		// Set colors and font
		graphElement.setBackgroundColor(CommonPreferenceConstants.getPreferenceCommentBackgroundColor(store));
		graphElement.setBorderColor(CommonPreferenceConstants.getPreferenceCommentBorderColor(store));
		graphElement.setFontColor(CommonPreferenceConstants.getPreferenceCommentFontColor(store));
		FontData[] fontdata = CommonPreferenceConstants.getPreferenceCommentFont(store);
		if (fontdata.length>0){
			graphElement.setFontFamily(fontdata[0].getName());
			graphElement.setFontSize(fontdata[0].getHeight());
		}

	}

	/**
	 * Gets the graph node size.
	 * 
	 * @return the graph node size
	 */
	public Dimension getGraphNodeSize() {
		return new Dimension(200,100);
	}
}
